﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Base.Components;
using System.ComponentModel;
using System.Windows.Forms;
using System.ComponentModel.Design.Serialization;
using ProgNetComponentsDx.Data.Design;
using ProgNetComponents.Data;

namespace ProgNetComponentsDx.Data
{
    public class SelectDataProvider : BaseExtenderProvider<SelectDataSettings>
    {
        public SelectDataProvider() { }
        public SelectDataProvider(IContainer container)
            : base(container)
        {

        }

        private Form _Form;

        [ReadOnly(true)]
        public Form Form
        {
            get { return _Form; }
        }

        protected override void ProcessObject(object target, SelectDataSettings settings)
        {
            
        }

        protected override SelectDataSettings CreateSettings(object target)
        {
            SelectDataSettings s = new SelectDataSettings(target, this);
            if (target is Form)
                _Form = target as Form;
            return s;
        }

        public override bool CanExtend(object extendee)
        {
            if (extendee is Form)
                return true;
            return false;
        }

        public bool FillDataSet(bool enforceConstraints = false)
        {
            if (Form != null && Form.Site != null && Form.Site.DesignMode)
                return true;

            SelectDataSettings s = GetSettings(Form);
            if (s != null && s.SelectDataId.IsNotNull() && s.DataSet != null)
            {
                s.DataSet.EnforceConstraints = enforceConstraints;
                bool res = true;
                try
                {
                    s.DataSet.EnforceConstraints = enforceConstraints;
                }
                catch
                {
                    res = false;
                    s.DataSet.EnforceConstraints = !enforceConstraints;
                }
                if (res)
                    res = SelectDataCache.FillData(s.DataSet, s.SelectDataId, s.Parameters.ToArray());
                if (res)
                    s.DataSet.AcceptChanges();
                return res;
            }
            
            return false;
        }

        public bool UpdateDataSet(bool enforceConstraints = true)
        {
            SelectDataSettings s = GetSettings(Form);
            if (s != null && s.SelectDataId.IsNotNull() && s.DataSet != null)
            {
                
                bool res = true;
                try
                {
                    s.DataSet.EnforceConstraints = enforceConstraints;
                }
                catch
                {
                    res = false;
                    s.DataSet.EnforceConstraints = !enforceConstraints;
                }
                if(res)
                    res = SelectDataCache.UpdateData(s.DataSet, s.SelectDataId);
                if (res)
                {
                    s.DataSet.AcceptChanges();
                    if (s.RefreshBrowserCache)
                    {
                        Browsers.BrowserCache.RefreshCache(s.DataSet);
                    }
                }
                
                return res;
            }
            return false;
        }

        public DataManager GetDataManager()
        {
            DataManager m = null;
            SelectDataSettings s = GetSettings(Form);
            if (s != null && s.SelectDataId.IsNotNull())
                m = SelectDataCache.GetManager(s.SelectDataId);
            return m;
        }
    }
}
